package com.yy.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.yy.common.annotation.Log;
import com.yy.common.constant.PmsConstants;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.enums.BusinessType;
import com.yy.common.utils.poi.ExcelUtil;
import com.yy.domain.PayDeposit;
import com.yy.service.IPayDepositService;
import com.yy.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;






/**
 * 押金Controller
 *
 * @author yy
 * @date 2023-04-11
 */
@RestController
@RequestMapping("/pay/deposit")
public class PayDepositController extends BaseController {
    @Autowired
    private IPayDepositService payDepositService;
    @Autowired
    private ISysConfigService configService;


    /**
     * 导出押金列表
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:export')")
    @Log(title = "押金", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayDeposit payDeposit) {
        List<PayDeposit> list = payDepositService.selectPayDepositList(payDeposit);
        ExcelUtil<PayDeposit> util = new ExcelUtil<PayDeposit>(PayDeposit.class);
        util.exportExcel(response, list, "押金数据");
    }

    /**
     * 查询押金列表
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:list')")
    @GetMapping("/list")
    public TableDataInfo list(PayDeposit payDeposit) {
        startPage();
        List<PayDeposit> list = payDepositService.selectPayDepositList(payDeposit);
        return getDataTable(list);
    }

    /**
     * 获取押金详细信息
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(payDepositService.selectPayDepositById(id));
    }

    /**
     * 新增押金
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:add')")
    @Log(title = "押金", businessType = BusinessType.INSERT)
    @PostMapping("addDeposit")
    public AjaxResult addDeposit(@RequestBody PayDeposit payDeposit) {
        //设置状态
        payDeposit.setState(PmsConstants.PAY_ORDER_STATE_0);
        //设置创建人
        payDeposit.setCreateBy(getUsername());
        //设置创建时间
        payDeposit.setCreateTime(new Date());
        //生成支付单号
        String prefix = configService.selectConfigByKey(PmsConstants.PREFIX_PAY_DEPOSIT_YJ);
        payDeposit.setPayNo(prefix + System.currentTimeMillis());

        return toAjax(payDepositService.insertPayDeposit(payDeposit));
    }

    /**
     * 押金退费
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:refund')")
    @Log(title = "押金退费", businessType = BusinessType.INSERT)
    @PostMapping("refundDeposit")
    public AjaxResult refundDeposit(@RequestBody PayDeposit payDeposit) {
        //取出退费方式
        String refundType = payDeposit.getRefundType();
        String remark = payDeposit.getRemark();
        //根据ID查询订单数据
        PayDeposit deposit = this.payDepositService.selectPayDepositById(payDeposit.getId());
        if (null == deposit) {
            return AjaxResult.error("当前订单号不存在");
        }
        if (!deposit.getState().equals(PmsConstants.PAY_ORDER_STATE_2)) {
            return AjaxResult.error("当前订单状态不是支付成功状态，不能进行退费操作");
        }
        deposit.setRefundType(refundType);
        deposit.setRemark(remark);
        deposit.setState(PmsConstants.PAY_ORDER_STATE_5);
        //设置退款人和退款时间
        deposit.setRefundUser(getUsername());
        deposit.setRefundTime(new Date());
        deposit.setRefundNo("DEPOSIT-REFUND"+System.currentTimeMillis());
        String prefix = configService.selectConfigByKey(PmsConstants.PREFIX_REFUND_DEPOSIT_YJ);
        deposit.setRefundNo(prefix + System.currentTimeMillis());

        return toAjax(payDepositService.updatePayDeposit(deposit));
    }

    /**
     * 确认收费
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:confirmPay')")
    @Log(title = "押金退费", businessType = BusinessType.INSERT)
    @PostMapping("confirmPay/{id}")
    public AjaxResult confirmPay(@PathVariable Long id) {
        //根据id查询订单数据
        PayDeposit payDeposit = this.payDepositService.selectPayDepositById(id);
        if (null == payDeposit) {
            return AjaxResult.error("当前订单号不存在");
        }
        if (!payDeposit.getState().equals(PmsConstants.PAY_ORDER_STATE_0)) {
            return AjaxResult.error("当前订单状态不是创建状态，不能确认收费");
        }
        //设置收款人
        payDeposit.setOperateUser(getUsername());
        //设置收款时间
        payDeposit.setOperateTime(new Date());
        //更新
        payDeposit.setState(PmsConstants.PAY_ORDER_STATE_2);
        return toAjax(this.payDepositService.updatePayDeposit(payDeposit));
    }



}

